/********************************************************************************
 * Project Name		[JavaEE_JDBC]
 * File Name     	[JdbcUtilsEnhanced.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package utils.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 增强的JDBC工具类<br/>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class JdbcUtilsEnhanced {

	private static DataSource dataSource = null;
	// 使用ThreadLocal存储当前线程中的Connection对象
	private static ThreadLocal<Connection> threadLocalConnection = new ThreadLocal<Connection>();

	static {
		try {
			// 使用c3p0创建数据源
			dataSource = new ComboPooledDataSource();
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	/**
	 * 获得连接
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		// 从当前线程中获得Connection
		Connection connection = threadLocalConnection.get();

		// 如果当前线程中没有Connection, 则从数据源中获取并绑定到当前线程
		if (connection == null) {
			connection = dataSource.getConnection();
			threadLocalConnection.set(connection);
		}

		return connection;
	}

	/**
	 * 开启事务
	 */
	public static void beginTransaction() {
		try {
			// 从当前线程中获得Connection
			Connection connection = threadLocalConnection.get();
			if (connection == null) {
				connection = getConnection();
				threadLocalConnection.set(connection);
			}

			// 开启事务
			connection.setAutoCommit(false);
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 回滚事务
	 */
	public static void rollback() {
		try {
			// 从当前线程中获得Connection
			Connection connection = threadLocalConnection.get();
			if (connection != null) {
				// 回滚事务
				connection.rollback();
			}
		} catch (SQLException e) {
			new RuntimeException(e.getSQLState(), e);
		}
	}

	/**
	 * 提交事务
	 */
	public static void commit() {
		try {
			// 从当前线程中获得Connection
			Connection connection = threadLocalConnection.get();
			if (connection != null) {
				// 提交事务
				connection.commit();
			}
		} catch (SQLException e) {
			new RuntimeException(e.getSQLState(), e);
		}
	}

	/**
	 * 关闭连接[并不是真正关闭, 而是把连接还给连接池]
	 */
	public static void close() {
		try {
			// 从当前线程中获得Connection
			Connection connection = threadLocalConnection.get();
			if (connection != null) {
				connection.close();
				// 解除当前线程绑定Connection
				threadLocalConnection.remove();
			}
		} catch (SQLException e) {
			new RuntimeException(e.getSQLState(), e);
		}
	}

	/**
	 * 获取数据源
	 * 
	 * @return
	 */
	public static DataSource getDataSource() {
		return dataSource;
	}
}
